package com.namecard.namecardapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.entity.DTO.LyUserReq;
import com.namecard.namecardapi.entity.DTO.PageDto;
import com.namecard.namecardapi.entity.LyUser;
import com.namecard.namecardapi.mapper.LyUserMapper;
import com.namecard.namecardapi.service.LyUserService;
import com.namecard.namecardapi.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 领英用户表(LyUser)表服务实现类
 *
 * @author makejava
 * @since 2024-08-06 13:38:34
 */
@Slf4j
@Service
@DS("sharding")
public class LyUserServiceImpl extends ServiceImpl<LyUserMapper, LyUser> implements LyUserService {

    @Override
    public void createIndexes() {
        log.info("领英索引创建中");
        baseMapper.createIndexes();
        log.info("领英索引创建成功");
    }

    public void dropIndexes() {
        baseMapper.dropIndexes();
        log.info("领英索引删除成功");
    }

    @Override
    public List<LyUser> getLikeFullName(String fullName) {
        LambdaQueryWrapper<LyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(LyUser::getFullName, fullName);
        return this.list(wrapper);
    }

    @Override
    public LyUser getFirstByEmail(String email) {
        LambdaQueryWrapper<LyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LyUser::getEmail, email);
        return this.getOnly(wrapper);
    }

    @Override
    public LyUser getFirstByMobile(String mobile) {
        LambdaQueryWrapper<LyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LyUser::getMobile, mobile);
        return this.getOne(wrapper);
    }

    @Override
    public List<LyUser> getLikeRightMobile(String mobile) {
        LambdaQueryWrapper<LyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(LyUser::getMobile, mobile);
        return this.list(wrapper);
    }

    @Override
    public List<LyUser> getByLocationCountry(String locationCountry) {
        LambdaQueryWrapper<LyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LyUser::getLocationCountry, locationCountry);
        return this.list(wrapper);
    }

    @Override
    public Page<LyUser> getByQuery(LyUserReq lyUserReq) {
        if (lyUserReq.getPageNum() == null) {
            lyUserReq.setPageNum(1);
        }
        if (lyUserReq.getPageSize() == null) {
            lyUserReq.setPageSize(10);
        }
        Page<LyUser> page = Page.of(lyUserReq.getPageNum(), lyUserReq.getPageSize());
        LambdaQueryWrapper<LyUser> wrapper = new LambdaQueryWrapper<>();
        //根据名字搜
        wrapper.eq(StringUtils.isNotBlank(lyUserReq.getFullName()), LyUser::getFullName, lyUserReq.getFullName());
        //根据邮箱搜
        wrapper.eq(StringUtils.isNotBlank(lyUserReq.getEmail()), LyUser::getEmail, lyUserReq.getEmail());
        //根据手机搜，如果符合中国手机号格式，精确搜，否则模糊搜
        if (StringUtils.isNotBlank(lyUserReq.getMobile())) {
            if (lyUserReq.getMobile().matches("^1[3-9]\\d{9}$")) {
                wrapper.eq(LyUser::getMobile, lyUserReq.getMobile());
            } else {
                wrapper.likeRight(LyUser::getMobile, lyUserReq.getMobile());
            }
        }
        //根据国家搜
        wrapper.eq(StringUtils.isNotBlank(lyUserReq.getLocationCountry()), LyUser::getLocationCountry, lyUserReq.getLocationCountry());
        //根据职位搜
        wrapper.like(StringUtils.isNotBlank(lyUserReq.getJobTitle()), LyUser::getJobTitle, lyUserReq.getJobTitle());
        //根据行业搜
        wrapper.like(StringUtils.isNotBlank(lyUserReq.getIndustry()), LyUser::getIndustry, lyUserReq.getIndustry());
        return this.page(page, wrapper);
    }

    public LyUser getOnly(LambdaQueryWrapper<LyUser> wrapper){
        wrapper.last("limit 1");
        return this.getOne(wrapper);
    };
}

